﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using YGWebMobileApi.Data;
using YGWebMobileApi.APITest.Config;
using System.Reflection;

namespace YGWebMobileApi.APITest
{
    public partial class ApiTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindApiList();
                rdoXml.Checked = true;
                BindCodeList();
            }
        }

        private void BindCodeList()
        {
          Type[] strustTypes=typeof(MessageCode).GetNestedTypes();
          foreach (var strustType in strustTypes)
          {
              FieldInfo[] fields=strustType.GetFields();
              lbxCodeList.Items.Add(fields[0].GetValue(null).ToString() + fields[1].GetValue(null).ToString());
          }
        }

        private void BindApiList()
        {
            MobileAPIDescription[] array = MobileAPIManager.GetAPIDescriptions();

            //var sorted = from m in array
            //             orderby m.Code ascending
            //             select m;

            var sorted = array.OrderBy(p => p.Code);

            if (sorted != null)
            {
                foreach (var dsp in sorted)
                {
                    ddlApiList.Items.Add(new ListItem(dsp.Code.PadRight(10) + dsp.Name, dsp.Code));
                }
                ddlApiList.SelectedIndex = 0;
                InitRequestText();
            }
        }

        private void InitRequestText()
        {
            string[] fields = MapApiInitXmlReader.GetAPINodeFields(ddlApiList.SelectedValue);
            if (fields != null)
            {
                MobileReqMsg req = new MobileReqMsg();
                req.Head.APICode = ddlApiList.SelectedValue;
                foreach (string name in fields)
                {
                    req.Body.AddField(name, string.Empty);
                }
                txtRequestMsg.Text = req.ToXml();
            }
            else
            {
                txtRequestMsg.Text = "请在 MapApiInit.xml 文件中添加 API 节点 " + ddlApiList.SelectedValue;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRequestMsg.Text.Trim()))
            {
                txtResponseMsg.Text = "请求的报文不能为空";
                return;
            }

            byte[] data = Encoding.UTF8.GetBytes(txtRequestMsg.Text.Trim());
            string url = this.Request.Url.ToString().Replace(this.Request.Url.AbsolutePath, "") + "/MobileApi.aspx";
            WebClient client = new WebClient();
            byte[] bytes = client.UploadData(url, "POST", data);
            txtResponseMsg.Text = Encoding.UTF8.GetString(bytes);
        }

        protected void ddlApiList_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtRequestMsg.Text = string.Empty;
            txtResponseMsg.Text = string.Empty;
            InitRequestText();
        }

        protected void rdoXml_CheckedChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtRequestMsg.Text.Trim()))
            {
                try
                {
                    txtRequestMsg.Text = MobileJsonConverter.ToXml(txtRequestMsg.Text.Trim()).ToXml();
                }
                catch (Exception ex)
                {
                    txtRequestMsg.Text = ex.ToString();
                }
            }

            if (!string.IsNullOrEmpty(txtResponseMsg.Text.Trim()))
            {
                try
                {
                    txtResponseMsg.Text = MobileJsonConverter.ToXml(txtResponseMsg.Text.Trim()).ToXml();
                }
                catch (Exception ex)
                {
                    txtResponseMsg.Text = ex.ToString();
                }
            }
        }

        protected void rdoJson_CheckedChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtRequestMsg.Text.Trim()))
            {
                try
                {
                    MobileXml mx = new MobileXml(txtRequestMsg.Text.Trim());
                    txtRequestMsg.Text = MobileJsonConverter.ToJosn(mx);
                }
                catch (Exception ex)
                {
                    txtRequestMsg.Text = ex.ToString();
                }
            }

            if (!string.IsNullOrEmpty(txtResponseMsg.Text.Trim()))
            {
                try
                {
                    MobileXml mx = new MobileXml(txtResponseMsg.Text.Trim());
                    txtResponseMsg.Text = MobileJsonConverter.ToJosn(mx);
                }
                catch (Exception ex)
                {
                    txtResponseMsg.Text = ex.ToString();
                }
            }
        }
    }
}